﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;
using NetTopologySuite.GeometriesGraph;

namespace NetTopologySuite.Operation.Overlay
{
	public class PointBuilder
	{
		public PointBuilder(OverlayOp op, IGeometryFactory geometryFactory)
		{
			this._op = op;
			this._geometryFactory = geometryFactory;
		}

		[Obsolete("point locateor no longer used!")]
		public PointBuilder(OverlayOp op, IGeometryFactory geometryFactory, PointLocator ptLocator = null) : this(op, geometryFactory)
		{
		}

		public IList<IGeometry> Build(SpatialFunction opCode)
		{
			this.ExtractNonCoveredResultNodes(opCode);
			return this._resultPointList;
		}

		private void ExtractNonCoveredResultNodes(SpatialFunction opCode)
		{
			foreach (Node node in this._op.Graph.Nodes)
			{
				if (!node.IsInResult && !node.IsIncidentEdgeInResult() && (node.Edges.Degree == 0 || opCode == SpatialFunction.Intersection) && OverlayOp.IsResultOfOp(node.Label, opCode))
				{
					this.FilterCoveredNodeToPoint(node);
				}
			}
		}

		private void FilterCoveredNodeToPoint(Node n)
		{
			Coordinate coordinate = n.Coordinate;
			if (!this._op.IsCoveredByLA(coordinate))
			{
				IPoint item = this._geometryFactory.CreatePoint(coordinate);
				this._resultPointList.Add(item);
			}
		}

		private readonly OverlayOp _op;

		private readonly IGeometryFactory _geometryFactory;

		private readonly List<IGeometry> _resultPointList = new List<IGeometry>();
	}
}
